<!-- 添加专家-详情资料 -->
<template>
  <publicPage officialAccount>
    <view class="addConsultPage" v-show="!loading">
      <InfoCard :item="infoObj" />
      <view class="mt-5 mb-20 mx-15" style="background-color: #f5f5f5">
        <u--textarea
          v-model="apText"
          placeholder="请输入内容"
          border="none"
          height="230rpx"
        ></u--textarea>
      </view>
      <view class="px-50 py-5">
        <u-button
          type="primary"
          text="立即签约"
          :customStyle="{
            fontSize: '32rpx',
            borderRadius: '20rpx',
            height: '88rpx',
          }"
          :throttleTime="500"
          @click="addFn"
        ></u-button>
      </view>
    </view>

    <NoLogin
      :show="loginShow"
      tips="点击立即登录,登录后即可申请签约"
      @close="loginShow = false"
      @loginReturn="getList"
    />

    <u-popup :show="editUserInfoShow" :round="10" :customStyle="{height: '720rpx'}" :closeOnClickOverlay="false" :safeAreaInsetBottom="false">
      <view class="linh-50 tc fz-16 fw-5 color8">请先完善用户信息</view>
      <MyInfo isComponents @editSuccess="editUserInfoShow = false" />
    </u-popup>

    <OfficialAccount :show="accountShow" @close="_closeFn" />
  </publicPage>
</template>

<script>
import InfoCard from "../components/infoCard.vue";
import MyInfo from "../myInfo/index.vue";
import orderApi from "@/api/order.js";
import NoLogin from "../components/noLogin.vue";
import OfficialAccount from "@/components/officialAccount.vue";

export default {
  components: {
    InfoCard,
    MyInfo,
    NoLogin,
    OfficialAccount
  },
  computed: {
    // 用户信息
    userInfo() {
      return this.$store.state.userInfo;
    }
  },
  data() {
    return {
      loading: true, // 是否加载中
      loginShow: false,
      getParams: {},
      infoObj: {},
      apText: "",
      editUserInfoShow: false,
      accountShow: false
    };
  },
  onLoad(options){
    const that = this
    if(options.scene){
      // 扫码进入
      that.getParams.exp_id = options.scene

      if(!that.$store.state.userInfo.token || !that.$store.state.userInfo.id){
        this.loginShow = true
        return
      }
    }else{
      that.getParams = options
    }
    that.getList()
  },
  methods: {
    // 获取专家详情
    async getList() {
      const res = await orderApi.isFriend({exp_id: this.getParams.exp_id})
      if(res.data.isFriend === 0){
        uni.redirectTo({
          url: `/pagesC/expHome/index?expertId=${this.getParams.exp_id}`
        });
        return
      }
      const { data } = await orderApi.getExpertInfo({expertId: this.getParams.exp_id})
      this.infoObj = data
      this.loading = false

      // 用户信息未完善
      if(!this.userInfo.userName || !this.userInfo.headimg || !this.userInfo.birthday){
        this.editUserInfoShow = true
      }
    },
    // 签约专家
    addFn() {
      const that = this
      let params = {
        exp_id: that.getParams.exp_id,
        apText: that.apText
      }
      orderApi.addExp(params).then(res=>{
        if(res.data && res.data.ap_id && res.data.apStatus === 0){
          uni.showToast({
            title: '提交成功，请等待专家同意。',
            icon: 'none',
            complete: ()=>{
              setTimeout(() => {
                if(!that.userInfo.mpOpenid){ // 未关注公众号
                  that.accountShow = true
                }else{
                  that.goBack()
                }
              }, 1500);
            }
          });
          // 发送申请签约 - 待通过
          that.$store.dispatch("sendCustomMessage",{
            type: 'contract',
            toID: that.getParams.exp_id,
            title: '申请签约专家',
            apText: that.apText,
            ap_id: res.data.ap_id
          });
        }else{
          // 已是好友
          uni.showToast({
            title: res.msg || res.data.msg,
            icon: 'none'
          });
          if(res.data && res.data.apStatus === 1){
            // 直接申请通过 - 返回列表直接刷新
            that.$store.commit("SET_ISRESET", true);
            setTimeout(() => {
              that.goBack()
            }, 1500);
          }
        }
      })
    },
    // 关闭公众号弹窗
    _closeFn(){
      this.accountShow = false
      this.goBack()
    }
  },
};
</script>
<style lang='scss' scoped>
.addConsultPage {
  height: 100%;
  background-color: #ffffff;
}
</style>
<style scoped>
.addConsultPage >>> .u-textarea {
  background-color: inherit !important;
}
</style>